/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vaccination;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.log4j.PropertyConfigurator;
import utils.DBController;
import utils.DBResultSet;
import utils.User;
import utils.Utils;

/**
 *
 * @author PacosHome
 */
public class Dottore_ListaConfermaPazienti extends HttpServlet {

    private static org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(Dottore_ListaConfermaPazienti.class); //avvio logger di log4j
    @Override
    public void init() {
        PropertyConfigurator.configure(Dottore_ListaConfermaPazienti.class.getClassLoader().getResource("utils/log4j.properties"));
    }
    
    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException {
        
        HttpSession session = request.getSession();
        User loggedUser = (User)session.getAttribute("user");
                
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        DBController dbController = DBController.getInstance();
        
        try {
            
            
            ArrayList<String> parameters = Utils.toList(request.getParameterNames());
            DBResultSet dbResult = dbController.getPatientsInfo(parameters);
            ResultSet patientsInfo = dbResult.getResultSet();
            Calendar cal = Calendar.getInstance();
            Timestamp date = new Timestamp(cal.getTimeInMillis());
            
            
            // salvo nella request la data di conferma
            session.setAttribute("dateVaccinations-"+loggedUser.getId(), date);
            // se non sono stati selezionati pazienti redireziono all'inizio della procedura
            if(patientsInfo == null) {
                response.sendRedirect("doctor/Dottore_ProceduraRichiamoPazienti");
                return;
            }
            
            Utils.printHeader(request,out,"Conferma richiami di vaccinazioni");
            out.println("           <div class=\"container-fluid-large\">\n" +
                        "               <div class=\"row-fluid\">\n" +
                        "                    <div class=\"span6\">\n" +
                        "                        <div class=\"well\">\n" +
                        "                            <ul class=\"nav nav-list\">\n" +
                        "                              <li class=\"nav-header\">Vaccini</li>\n" +
                        "                              <li><a href=\"../dottore_proceduraRichiamoPazienti.html\">Procedura Richiamo Pazienti</a></li>\n" +
                        "                              <li><a href=\"javascript:history.back()\">- Lista</a></li>\n" +
                        "                              <li class=\"active\"><a href=\"#\">- Conferma</a></li>\n" +
                        "                              <hr>" +
                        "                              <li><a href=\"../dottore_visualizzaRichiamati.html\">Visualizza Pazienti Richiamati</a></li>\n" +
                        "                              <li class=\"nav-header\">Utente</li>\n" +
                        "                              <li><a href=\"Home\">Home</a></li>\n" +
                        "                              <li><a href=\"#\">Modifica Dati</a></li>\n" +
                        "                              <li><a href=\"Logout\">Logout</a></li>\n" +
                        "                            </ul>\n" +
                        "                        </div>\n" +
                        "                    </div>\n" +
                        "                    <div class=\"well span6\" style=\"padding:10px;\">\n" +
                        "                       <div>\n");
            
            // stampo la tabella con i pazienti precedentemente selezionati per la vaccinazione
            out.println("                           <h1>Richiami</h1>\n" +
                        "                           <form action=\"Dottore_PazientiConfermati\" method=\"post\">\n" +
                        "                               <table class=\"table\">\n" +
                        "                               <tr>\n" +
                        "                                   <td width=\\\"120\\\"><h4>Vuoi registrare le vaccinazioni in data "+Utils.formatDateTime(date) +"?</h4></td>\n "+
                        "                                   </tr>\n" +
                        "                                   <tr>\n" +
                        "                                       <td width=\"120\"><h6>Pazienti</h6></td>\n" +
                        "                                   </tr>\n");
            
            boolean evenline= true;
            while(patientsInfo.next()) {
                if(evenline) {
                    out.println("                               <tr style=\"background:#cccccc;\">\n");
                }
                else {
                    out.println("                               <tr>\n"); 
                }
                    
                    out.println("                                   <td><a data-toggle=\"modal\" href=\"#infoPaziente"+ patientsInfo.getString("id") +"\">" + patientsInfo.getString("name") + " " + patientsInfo.getString("surname") + "</a></td>" +
                                "                                   <input type=\"hidden\" name=\""+ patientsInfo.getString("id") + "\" />" +
                                "                                   <div id=\"infoPaziente"+ patientsInfo.getString("id") +"\" class=\"modal hide fade\" tabindex=\"-1\" role=\"dialog\" aria-labelledby=\"myModalLabel\" aria-hidden=\"true\" style=\"display: none;\">\n" +
                                "                                      <div align=\"center\">\n" +
                                "                                          <div class=\"modal-header\">\n" +
                                "                                              <button type=\"button\" class=\"close\" data-dismiss=\"modal\" aria-hidden=\"true\">x</button>\n" +
                                "                                              <br>\n" +
                                "                                              <br>\n" +
                                "                                              <h3 id=\"myModalLabel\">Scheda Paziente</h3>\n" +
                                "                                          </div>\n" +
                                "                                          <div class=\"modal-body\">\n" +
                                "                                              <h4>" + patientsInfo.getString("name") + " " + patientsInfo.getString("surname") + "</h4>\n" +
                                "                                              <p>"+ patientsInfo.getString("birthdate") +"</p>\n" +
                                "                                              <img src=\"../images/"+ patientsInfo.getString("photo") +"\">\n" +
                                "                                          </div>\n" +
                                "                                      </div>\n" +
                                "                                  </div>\n"+
                                "                               </tr>\n");
                    
                
                
                evenline = !evenline;
            }
            dbController.releaseConnection(dbResult);
            
            out.println("                               </table>\n" +
                        "                               <br>\n" +
                        "                               <a href=\"../dottore_proceduraRichiamoPazienti.html\">\n" +
                        "                                   <button class=\"btn btn-mini\" type=\"submit\">Cancella</button>\n" +
                        "                               </a>\n" +
                        "                               <input class=\"btn btn-primary btn-mini\" type=\"submit\" name=\"conferm\" value=\"Conferma\"/>\n" +
                        "                            </form>\n");  
            
            out.println("        <!-- Il javascript -->\n" +
                        "        <script src=\"../styles/jquery.js\"></script>\n" +
                        "        <script src=\"../styles/javascript.js\"></script>\n");
            
            Utils.printFooter(out);
            
        }
        catch(Exception e){
            ///stampa tramite log4j il messaggio dell'eccezione
            log.error(e.getMessage());
            String message = e.getMessage()+"" +
                    "<br>" +
                    "<br>" + 
                    "<a href=\"login.html\">\n" +
                    "<button class=\"btn btn-primary btn-danger\">Back</button> \n" +
                    "</a>";
            
            Utils.printHeader(request, out, "Errore!");
            Utils.printError(request, out, message);
            Utils.printFooter(out);
        }
        finally {
            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(Dottore_ListaConfermaPazienti.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(Dottore_ListaConfermaPazienti.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
